package com.kawaiior.asoulworld.items.weapon;

import com.kawaiior.asoulworld.capability.AsoulAirPower;
import com.kawaiior.asoulworld.capability.CapabilityRegistryHandler;
import com.kawaiior.asoulworld.items.AsWeaponBase;
import com.kawaiior.asoulworld.potion.ModPotions;
import com.kawaiior.asoulworld.util.EntityUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.MobEffects;
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.List;

public class ItemLegendSword extends AsWeaponBase {

    public ItemLegendSword(String name) {
        super(name);
        this.attack_damage = 11f;
        this.max_damage = 1024;
    }

    @Override
    public EnumAction getItemUseAction(ItemStack stack)
    {
        return EnumAction.NONE;
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
        if (worldIn.isRemote) return super.onItemRightClick(worldIn, playerIn, handIn);

        playerIn.setActiveHand(handIn);
        Entity pointedEntity = getPlayerLookTarget(worldIn, playerIn);
        if (pointedEntity instanceof EntityLivingBase){
            EntityLivingBase living = (EntityLivingBase) pointedEntity;
            double posX = playerIn.posX;
            double posY = 200D;
            double posZ = playerIn.posZ;

            if (worldIn.provider.getDimension() == -1){
                posY = 126D;
            }

            for (int i = 0; i < 12; i++) {
                for (int j = 0; j < 12; j++) {
                    worldIn.setBlockState(new BlockPos(posX+i,posY,posZ+j), Blocks.GLOWSTONE.getDefaultState());
                    worldIn.setBlockToAir(new BlockPos(posX+i,posY+1,posZ+j));
                    worldIn.setBlockToAir(new BlockPos(posX+i,posY+2,posZ+j));
                    if (i==0 || i==11 || j==0 || j==11){
                        worldIn.setBlockState(new BlockPos(posX+i,posY+1,posZ+j), Blocks.NETHER_BRICK_FENCE.getDefaultState());
                    }
                }
            }


            double px;
            double pz;
            double lx;
            double lz;
            if (playerIn.posX > living.posX){
                px = posX + 10;
                lx = posX + 1;
            }
            else {
                px = posX + 1;
                lx = posX + 10;
            }

            if (playerIn.posZ > living.posZ){
                pz = posZ + 10;
                lz = posZ + 1;
            }
            else {
                pz = posZ + 1;
                lz = posZ + 10;
            }

            AsoulAirPower power =  playerIn.getCapability(CapabilityRegistryHandler.ASOUL_AIR_POWER, null);
            if (power == null){
                return super.onItemRightClick(worldIn, playerIn, handIn);
            }
            power.setHealth(20);
            power.setXYZ(posX,posY,posZ);

            EntityUtil.ApplyBuff(playerIn, ModPotions.AS_FALL_IGNORE,0,120f);
            EntityUtil.ApplyBuff(playerIn, MobEffects.STRENGTH,0,15f);
            EntityUtil.ApplyBuff(living, ModPotions.AS_FALL_IGNORE, 0, 120f);

            playerIn.setPositionAndUpdate(px,posY+1,pz);
            living.setPositionAndUpdate(lx,posY+1,lz);

            playerIn.getCooldownTracker().setCooldown(this, 700);
        }

        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    @Nullable
    private Entity getPlayerLookTarget(World world, EntityLivingBase living) {
        Entity pointedEntity = null;
        double range = 128D;
        Vec3d srcVec = new Vec3d(living.posX, living.posY + living.getEyeHeight(), living.posZ);
        Vec3d lookVec = living.getLook(1.0F);
        Vec3d destVec = srcVec.addVector(lookVec.x*range,lookVec.y*range,lookVec.z*range);
        float var9 = 1.0F;
        List<Entity> possibleList = world.getEntitiesWithinAABBExcludingEntity(living, living.getEntityBoundingBox().expand(lookVec.x * range, lookVec.y * range, lookVec.z * range).grow(var9, var9, var9));
        double hitDist = 0;

        for (Entity possibleEntity : possibleList) {
            if (possibleEntity.canBeCollidedWith()) {
                float borderSize = possibleEntity.getCollisionBorderSize();
                AxisAlignedBB collisionBB = possibleEntity.getEntityBoundingBox().grow((double) borderSize, (double) borderSize, (double) borderSize);
                RayTraceResult interceptPos = collisionBB.calculateIntercept(srcVec, destVec);

                if (collisionBB.contains(srcVec)) {
                    if (0.0D < hitDist || hitDist == 0.0D) {
                        pointedEntity = possibleEntity;
                        hitDist = 0.0D;
                    }
                } else if (interceptPos != null) {
                    double possibleDist = srcVec.distanceTo(interceptPos.hitVec);

                    if (possibleDist < hitDist || hitDist == 0.0D) {
                        pointedEntity = possibleEntity;
                        hitDist = possibleDist;
                    }
                }
            }
        }
        return pointedEntity;
    }
}
